using Framework.EventArg;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
/// <summary>
/// 事件中心
/// </summary>
public static class EventCenter
{
    static  Dictionary<int, List<Action<object[]>>> evtCtr = new Dictionary<int, List<Action<object[]>>>();//事件中心

    static  Dictionary<int, List<Action<EventArg>>> dataEvtDic = new Dictionary<int, List<Action<EventArg>>>();
    static  Dictionary<int, List<Action<EventArg>>> uiEvtDic = new Dictionary<int, List<Action<EventArg>>>();

    #region 添加监听
    /// <summary>
    /// 添加监听
    /// </summary>
    /// <param name="def">事件订阅类型</param>
    /// <param name="handle">事件</param>
    public static void AddListener(int def, Action<object[]> handle)
    {
        if (!evtCtr.ContainsKey(def))
        {
            evtCtr.Add(def, new List<Action<object[]>>());
            if (handle != null)
            {
                evtCtr[def].Add(handle);
            }

        }
        else if (!evtCtr[def].Contains(handle))
            evtCtr[def].Add(handle);
        //Debug.Log("添加事件订阅:" + def.ToString());
    }

    /// <summary>
    /// 添加数据-UI事件监听
    /// </summary>
    /// <param name="def"></param>
    /// <param name="handle"></param>
    public static void AddDataListener(int def, Action<EventArg> handle)
    {

        //Debug.Log(def);
        if (!dataEvtDic.ContainsKey(def))
        {
            dataEvtDic.Add(def, new List<Action<EventArg>>());
            if (handle != null)
            {
                dataEvtDic[def].Add(handle);
            }

        }
        else if (!dataEvtDic[def].Contains(handle))
            dataEvtDic[def].Add(handle);
    }   
    /// <summary>
    /// 添加UI-UI事件监听
    /// </summary>
    /// <param name="def"></param>
    /// <param name="handle"></param>
    public static void AddUIListener(int def, Action<EventArg> handle)
    {

        if (!uiEvtDic.ContainsKey(def))
        {
            uiEvtDic.Add(def, new List<Action<EventArg>>());
            if (handle != null)
            {
                uiEvtDic[def].Add(handle);
            }

        }
        else if (!uiEvtDic[def].Contains(handle))
            uiEvtDic[def].Add(handle);
    }


    #endregion


    #region 移除监听

 
    /// <summary>
    /// 移除某事件类型下的某个监听
    /// </summary>
    /// <param name="def">事件订阅类型</param>
    public static void RemoveListener(int def, Action<object[]> handle)
    {
        if (evtCtr.ContainsKey(def) && evtCtr[def].Contains(handle))
            evtCtr[def].Remove(handle);

    }


    /// <summary>
    /// 移除某数据-UI事件类型下的某个监听
    /// </summary>
    /// <param name="def">事件订阅类型</param>
    public static void RemoveDataListener(int def, Action<EventArg> handle)
    {


        if (dataEvtDic.ContainsKey(def) && dataEvtDic[def].Contains(handle))
            dataEvtDic[def].Remove(handle);

    }   
    
    /// <summary>
    /// 移除某事件类型下的某个监听
    /// </summary>
    /// <param name="def">事件订阅类型</param>
    public static void RemoveUIListener(int def, Action<EventArg> handle)
    {
        if (uiEvtDic.ContainsKey(def) && uiEvtDic[def].Contains(handle))
            uiEvtDic[def].Remove(handle);

    }


    /// <summary>
    /// 移除所有监听
    /// </summary>
    /// <param name="def">事件订阅类型</param>
    public static void RemoveAllListener(int def)
    {
        if (evtCtr.ContainsKey(def))
        {
            evtCtr.Remove(def);
        }
    }



    #endregion



    #region 调用监听


    /// <summary>
    /// 触发事件
    /// </summary>
    /// <param name="def">事件订阅类型</param>
    /// <param name="arg">参数</param>
    public static void Call(int def, params object[] arg)
    {
        if (evtCtr.ContainsKey(def))
        {
            if (evtCtr[def]!=null)
            {
/*                if (def.Equals(EventDef.AdReward))
                {
                    Debug.Log(evtCtr[def].Count);
                }*/
                for (int i = evtCtr[def].Count - 1; i >= 0; i--)
                {
                    evtCtr[def][i](arg);
                }
/*                foreach (var item in evtCtr[def])
                {
                    item(arg);
                }*/
            }
           
        }
    }

    /// <summary>
    /// 触发数据-UI事件
    /// </summary>
    /// <param name="def">事件订阅类型</param>
    /// <param name="arg">参数</param>
    public static void CallDataListener(int def, params object[] arg)
    {
        if (dataEvtDic.ContainsKey(def))
        {
            if (dataEvtDic[def] != null)
            {
/*                if (def.Equals(DataListenerType.CollectionDataUpdate))
                {
                    Debug.Log(dataEvtDic[def].Count);
                }*/
                for (int i = dataEvtDic[def].Count - 1; i >= 0; i--)
                {
                    dataEvtDic[def][i].Invoke(new EventArg(arg));
                }
                /*                foreach (var item in evtCtr[def])
                                {
                                    item(arg);
                                }*/
            }

        }
    } 
    
    /// <summary>
    /// 触发UI-UI事件
    /// </summary>
    /// <param name="def">事件订阅类型</param>
    /// <param name="arg">参数</param>
    public static void CallUIListener(int def, params object[] arg)
    {
        if (uiEvtDic.ContainsKey(def))
        {
            if (uiEvtDic[def] != null)
            {
                for (int i = uiEvtDic[def].Count - 1; i >= 0; i--)
                {
                    uiEvtDic[def][i].Invoke(new EventArg(arg));
                }
            }

        }
    }

    /// <summary>
    /// 触发事件
    /// </summary>
    /// <param name="def">事件订阅类型</param>
    /// <param name="arg">参数</param>
    public static void Call(int def, params Action[] arg)
    {
        if (evtCtr.ContainsKey(def))
        {
            if (evtCtr[def]!=null)
            {
                foreach (var item in evtCtr[def])
                {
                    item(arg);
                }
            }
           
        }
    }

    #endregion

}
